package net.oschina.foodon.examples.spring.boot.amqp.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.stereotype.Component;

/**
 * MQ成功接收后回调
 */
@Component
public class MessageConfirmCallback implements RabbitTemplate.ConfirmCallback {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    /*
     * 消息发送确认回调方法
     * 实现RabbitTemplate.ConfirmCallback接口
     * 注意，消息回调只能代表消息发送成功，不能代表消息被成功处理
     * @param correlationData correlation data for the callback.
     * @param ack true for ack, false for nack
     * @param cause An optional cause, for nack, when available, otherwise null.
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            logger.info("消息发送成功.");
        }

        //失败处理
        if (!ack) {
            logger.warn("消息发送失败, ID为: {}, 原因为: {}.", correlationData.getId(), cause);
        }
    }
}